package com.mysky.client.mvp.audioplayer.soundplayer;

import com.chj.gwt.client.soundmanager2.Callback;
import com.chj.gwt.client.soundmanager2.SMSound;
import com.chj.gwt.client.soundmanager2.SoundManager;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.mysky.client.mvp.audioplayer.event.MDErrorEvent;
import com.mysky.client.mvp.audioplayer.event.MDPauseResumeEvent;
import com.mysky.client.mvp.audioplayer.event.MDPlayEvent;
import com.mysky.client.mvp.audioplayer.event.MDPositionEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongFinishedEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongPausedEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongReadyEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongStartedEvent;

public class SMSoundPlayer implements SoundPlayer {

	private static final String SOUND_ID = "soundID";

	/* SoungManager and player related variables */
	// SoundManger to play our sound
	private SoundManager sm;

	Boolean isPaused = false;

	private EventBus eventBus;

	@Inject
	public SMSoundPlayer(EventBus eventBus) {
		sm = SoundManager.getInstance();
		sm.setFlashVersion(9);
		sm.setDebugMode(true);
		sm.beginDelayedInit();
		this.eventBus = eventBus;
		initEventHandler();
	}
	
	private void initEventHandler() {
		eventBus.addHandler(MDPlayEvent.TYPE, new MDPlayEvent.MDPlayEventHandler() {
			@Override
			public void handleEvent(MDPlayEvent event) {
				playSong(event.getUrl());
			}
		});
		eventBus.addHandler(MDPauseResumeEvent.TYPE, new MDPauseResumeEvent.MDPauseResumeEventHandler() {
			@Override
			public void handleEvent(MDPauseResumeEvent event) {
				if( isPaused ) {
					resumePlaying();
				} else {
					pausePlaying();
				}
			}
		});
		eventBus.addHandler(MDPositionEvent.TYPE, new MDPositionEvent.MDPositionEventHandler() {
			@Override
			public void handleEvent(MDPositionEvent event) {
				setPctPosition(event.getPercent());
			}
		});
		
	}

	public void playSong(String song) {
		if (!sm.getSoundIDs().isEmpty()) {
			sm.destroySound(SOUND_ID);
		}

		sm.getDefaultOptions().onFinish(new Callback() {
			@Override
			public void execute() {
				eventBus.fireEvent(new MDSongFinishedEvent());
			}
		});
		sm.onReady(new Callback() {
			@Override
			public void execute() {
				eventBus.fireEvent(new MDSongReadyEvent());
			}
		});
		sm.onTimeout(new Callback() {
			@Override
			public void execute() {
				eventBus.fireEvent(new MDErrorEvent());
			}
		});

		System.out.println("playing song " + song);
		sm.createSound(SOUND_ID, song);
		sm.play(SOUND_ID);
		
		eventBus.fireEvent(new MDSongStartedEvent());
		isPaused = false;
	}

	public void stopPlaying() {
		System.out.println("Stopping all music!");
		sm.stopAll();
		eventBus.fireEvent(new MDSongFinishedEvent());
	}

	public boolean isSoundPlaying() {
		SMSound track = sm.getSoundById(SOUND_ID);
		return ((track != null) && (track.getPlayState() == 1));
	}

	public void pausePlaying() {
		sm.pause(SOUND_ID);
		isPaused = true;
		eventBus.fireEvent(new MDSongPausedEvent());
	}

	public void resumePlaying() {
		sm.resume(SOUND_ID);
		isPaused = false;
		eventBus.fireEvent(new MDSongStartedEvent());
	}

	public void setPlayingCallback(final PlayingCallback playing) {
		if (sm == null) {
			return;
		}
		sm.defaultOptions
				.whilePlaying(new com.chj.gwt.client.soundmanager2.Callback() {

					public void execute() {
						playing.fire(sm.getSoundById(SOUND_ID).getPosition(),
								sm.getSoundById(SOUND_ID).getDurationEstimate());
					}

				});
	}

	public Boolean getIsPaused() {
		return isPaused;
	}
	
	public void setPctPosition(int percent) {
		int duration = sm.getSoundById(SOUND_ID).getDuration();
		int milliseconds = duration*percent/100*1000;
		sm.setPosition(SOUND_ID, milliseconds);
	}

}
